package com.pansoft.openplanet.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.zxing.qrcode.QRCode;
import com.efounder.constant.EnvironmentVariable;
import com.pansoft.openplanet.R;
import com.utilcode.util.ActivityUtils;

import org.json.JSONException;
import org.json.JSONObject;

import static com.efounder.frame.utils.Constants.CHAT_USER_ID;

public class EnergyQRCodeActivity extends TCBaseActivity implements View.OnClickListener {

    private ImageView qrCodeIv;
    private int height = 800;
    private int width = 800;
    private String jsonResult;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_energyexchage_qr_layout);

        String ethAddress = EnvironmentVariable.getProperty("tc_ethAddress", "");
//        jsonResult = getJSONResult("gathering", EnvironmentVariable.getUserName(), ethAddress, "0");
        jsonResult = getJSONResult("gathering", EnvironmentVariable.getProperty(CHAT_USER_ID), ethAddress, "0");

        initFindView();


    }

    private String getJSONResult(String type, String userId, String walletAddress, String money) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("type", type);
            jsonObject.put("userId", userId);
            jsonObject.put("ethAddress", walletAddress);
            jsonObject.put("money", money);

            return jsonObject.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }

    private void initFindView() {

        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.exchangeRecordTv).setOnClickListener(this);
        ((TextView) findViewById(R.id.tv_title)).setText(getResources().getString(R.string.open_planet_energy_exchange));
        qrCodeIv = (ImageView) findViewById(R.id.qrCodeIv);
        findViewById(R.id.settingExchangeCountTv).setOnClickListener(this);
        qrCodeIv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                width = qrCodeIv.getWidth();
                height = qrCodeIv.getHeight();
                putQRCodeIV();
                qrCodeIv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

    private void putQRCodeIV() {
        Bitmap bitmap = QRCode.setQRcode(jsonResult, width, height);
        qrCodeIv.setImageBitmap(bitmap);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {
            finish();
        } else if (v.getId() == R.id.settingExchangeCountTv) {
            //跳转到设置金额界面
            Intent intent = new Intent(EnergyQRCodeActivity.this, SetExchangeNumberActivity.class);
            startActivityForResult(intent, 0);
        } else if (v.getId() == R.id.exchangeRecordTv) {
            //跳转到设置金额界面
            Intent intent = new Intent(EnergyQRCodeActivity.this, ExchangeRecordListActivity.class);
            startActivityForResult(intent, 0);
        }
    }

    public static void open() {
        ActivityUtils.startActivity(EnergyQRCodeActivity.class);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && data != null) {
            String money = data.getStringExtra("money");

            String ethAddress = EnvironmentVariable.getProperty("tc_ethAddress", "");
            jsonResult = getJSONResult("gathering", EnvironmentVariable.getProperty(CHAT_USER_ID), ethAddress, money);
            putQRCodeIV();
        }
    }


}
